package cn.jekun.shop.web.action;
import javax.annotation.Resource;

import net.sf.json.JSONObject;
import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.shop.common.service.ShopBaseAction;
import cn.jekun.shop.web.service.HistoryService;
/**
 * 浏览过的商品
 * @author twj
 *
 */
@SuppressWarnings("serial")
public class HittedAction extends ShopBaseAction{
	@Resource(name = HistoryService.BEAN_NAME)
	protected HistoryService historyService;
	/**
	 * 最近浏览过的商品
	 * @return
	 */
	public String history(){
		JSONObject jsonObject = null;
		//验证是否登录
		if(!isLogin()){
			return timeout();
		}
		try{
			String userId = this.request.getParameter("userId");
			jsonObject = historyService.query(userId);
		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
			return this.writeJson(jsonObject);
		}
	/**
	 * 根据cookie，获取商品信息
	 * @return
	 */
	public String queryHis(){
		JSONObject jsonObject = null;
		try{
			String val = this.request.getParameter("val");
			jsonObject = this.historyService.queryHis(val);
		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
}
